home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Tools & Apps / Networking & Communications / Serial NB Sample Driver / Task / inc / control.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  7.6 KB  |  478 lines  |  [TEXT/MPS ]

  1. /********************************************************************************/
  2. /*                                                                                */
  3. /*        control.c - control request processing.                                    */
  4. /*                                                                                */
  5. /*        Richard W. Mincher.  February 19, 1990.                                    */
  6. /*                                                                                */
  7. /*        Copyright © 1990 Apple Computer, Inc.  All rights reserved.                */
  8. /*                                                                                */
  9. /********************************************************************************/
  10.  
  11. #include    "AROSE.h"
  12. #include    "os.h"
  13. #include    "managers.h"
  14.  
  15. #include    "ARDriver.h"
  16. #include    "ARTask.h"
  17.  
  18. static unsigned short    csStuff[12];
  19.  
  20. ControlCall()
  21. {    
  22.     NetCopy( msg->mFrom, msg->mDataPtr, msg->mTo, csStuff, 24 );
  23. #ifdef    DEBUG
  24.     printf("Control received.  mDataPtr=%x, mDataSize=%x, csCode=%04.4x\n",
  25.         msg->mDataPtr, msg->mDataPtr, csStuff[0] );
  26. #endif    DEBUG
  27.     msg->mStatus = 0;
  28.     switch( csStuff[0] )
  29.     {
  30.         case    1:    /*    KillIO            */
  31.             KillIO();
  32.             break;
  33.         case    8:    /*    Reinitialize    */
  34.             Reconfigure();
  35.             break;
  36.         case    9:    /*    New Buffer        */
  37.             break;
  38.         case    10:    /*    Hankshake        */
  39.             SetHandShake();
  40.             break;
  41.         case    11:    /*    Clear Break        */
  42.             ClearBreak();
  43.             break;
  44.         case    12:    /*    Set Break        */
  45.             SetBreak();
  46.             break;
  47.         case    13:    /*    Baud Rate        */
  48.             SetBaudRate();
  49.             break;
  50.         case    14:    /*    More Handshake    */
  51.             SetHandShake();
  52.             break;
  53.         case    16:    /*    Misc. Options    */
  54.             SetMiscOpt();
  55.             break;
  56.         case    17:    /*    Assert DTR        */
  57.             AssertDTR();
  58.             break;
  59.         case    18:    /*    Negate DTR        */
  60.             NegateDTR();
  61.             break;
  62.         case    19:    /*    Parity Replace    */
  63.             SetPEChar();
  64.             break;
  65.         case    20:    /*    Parity w/ Alt    */
  66.             SetPEAltChar();
  67.             break;
  68.         case    21:    /*    Force XOFF        */
  69.             ForceXOff();
  70.             break;
  71.         case    22:    /*    Clear XOFF        */
  72.             ClearXOff();
  73.             break;
  74.         case    23:    /*    XON if XOFF        */
  75.             SendXOnIff();
  76.             break;
  77.         case    24:    /*    Send XON        */
  78.             SendXOn();
  79.             break;
  80.         case    25:    /*    XOFF if XON        */
  81.             SendXOffIff();
  82.             break;
  83.         case    26:    /*    Send XOFF        */
  84.             SendXOff();
  85.             break;
  86.         case    27:    /*    Reset            */
  87.             ResetSCC();
  88.             break;
  89.         default:
  90.             msg->mStatus = -17;    /* controlErr    */
  91.     }
  92. }
  93.  
  94.  
  95. //
  96. //    Control 1 - Kill IO.
  97. //
  98.  
  99. KillIO()
  100. {
  101.     short        s;
  102.     message        *m;
  103.     tid_type    temp;
  104.     
  105. //    Reset SCC pointers.
  106.  
  107.     s = Spl(7);
  108.     G->txCount = G->txTickle = 0;
  109.     G->txIn = G->txOut = G->txFirst;
  110.     G->rxCount = G->rxMax;
  111.     G->rxTickle = 0;
  112.     G->rxIn = G->rxOut = G->rxFirst;
  113.     G->sRxCount = 0;
  114.     G->sTxCount = 0;
  115.     (void)Spl(s);
  116.     
  117. //    Send back write requests.
  118.  
  119.     while(m = G->txQHead)
  120.     {
  121.         G->txQHead = G->txQHead->mNext;
  122.         temp = m->mFrom;
  123.         m->mFrom = m->mTo;
  124.         m->mTo = temp;
  125.         m->mCode |= 1;
  126.         m->mStatus = -27;
  127.         Send( m );
  128.     }
  129.     
  130. //    Send back read requests.
  131.  
  132.     while(m = G->rxQHead)
  133.     {
  134.         G->rxQHead = G->rxQHead->mNext;
  135.         temp = m->mFrom;
  136.         m->mFrom = m->mTo;
  137.         m->mTo = temp;
  138.         m->mCode |= 1;
  139.         m->mStatus = -27;
  140.         Send( m );
  141.     }
  142.     
  143. }
  144.  
  145. //
  146. //    Control 8 - Reconfigure.
  147. //
  148. Reconfigure()
  149. {
  150.     short    baudConst, s;
  151.     
  152.     s = Spl(7);
  153.     baudConst = (((csStuff[1] & 0x3FF) + 2) << 1) - 2;
  154.     G->baudLow = baudConst & 0xFF;
  155.     G->baudHigh = (baudConst >> 8) & 0xFF;
  156.     G->lastWR4 = ((csStuff[1] >> 12) & 0x0F) | 0x40;
  157.     G->lastWR3 = ((csStuff[1] >> 4) & 0xC0) | 0x01;
  158.     G->lastWR5 = ((csStuff[1] >> 5) & 0x60) | 0x08;
  159.     switch( csStuff[1] & 0xC00 )
  160.     {
  161.         case    0x000:
  162.             G->charMask = 0x1F;
  163.             break;
  164.         case    0x400:
  165.             G->charMask = 0x7F;
  166.             break;
  167.         case    0x800:
  168.             G->charMask = 0x3F;
  169.             break;
  170.         case    0xC00:
  171.             G->charMask = 0xFF;
  172.             break;
  173.     }
  174.     (void)Spl(s);
  175. #ifdef    DEBUG
  176.     printf("Low=%x; High=%x; wr3=%x; wr4=%x; wr5=%x\n", 
  177.         G->baudLow, G->baudHigh, G->lastWR3, G->lastWR4, G->lastWR5 );
  178. #endif    DEBUG
  179.     InitSCC();
  180. }
  181.  
  182. //
  183. //    Control 10/14 - Set handshake options.
  184. //
  185.  
  186. SetHandShake()
  187. {
  188.     short    s;
  189.  
  190.     s = Spl(7);
  191.     G->swhs = (csStuff[1] >> 8) & 0xFF;
  192.     G->hwhs = csStuff[1] & 0xFF;
  193.     G->xOnChar = (csStuff[2] >> 8) & 0xFF;
  194.     G->xOffChar = csStuff[2] & 0xFF;
  195.     G->options = (csStuff[3] >> 8) & 0xFF;
  196.     G->postOptions = csStuff[3] & 0xFF;
  197.     G->inSwHs = (csStuff[4] >> 8) & 0xFF;
  198.     G->inHwHs = csStuff[4] & 0xFF;
  199.     (void)Spl(s);
  200.     if ((csStuff[0] == 14) && G->inHwHs)
  201.         AssertDTR();
  202. #ifdef    DEBUG
  203.     printf("swhs=%x; hwhs=%x; xOnChar=%x; xOffChar=%x\n",
  204.         G->swhs, G->hwhs, G->xOnChar, G->xOffChar );
  205.     printf("options=%x;postOptions=%x;inSwHs=%x;inHwHs=%x\n",
  206.         G->options, G->postOptions, G->inSwHs, G->inHwHs );
  207. #endif    DEBUG
  208.     
  209. }
  210.  
  211. //
  212. //    Control 11 - Clear BREAK.
  213. //
  214. ClearBreak()
  215. {
  216.     short    s;
  217.     
  218.     s = Spl(7);
  219.     *SCCControl = 0x05;
  220.     *SCCControl = G->lastWR5;
  221.     (void)Spl(s);
  222. }
  223.  
  224. //
  225. //    Control 12 - Set BREAK.
  226. //
  227. SetBreak()
  228. {
  229.     short    s;
  230.     
  231.     s = Spl(7);
  232.     *SCCControl = 0x05;
  233.     *SCCControl = G->lastWR5 | 0x10;
  234.     (void)Spl(s);
  235. }
  236.  
  237. //
  238. //    Control 13 - Set Baud Rate.
  239. //
  240. SetBaudRate()
  241. {
  242.     short    baudConst;
  243.     
  244.     baudConst = (SNBCLOCK / (32 * csStuff[1])) - 2;
  245.     G->baudLow = baudConst & 0xFF;
  246.     G->baudHigh = (baudConst >> 8) & 0xFF;
  247.     InitSCC();
  248. }
  249.  
  250. //
  251. //    Control 16 - Set Misc. Options.
  252. //
  253. SetMiscOpt()
  254. {    
  255.     G->ctlOptions = (csStuff[1] >> 8) & 0xFF;
  256. }
  257.  
  258. //
  259. //    Control 17 - Assert DTR.
  260. //
  261. AssertDTR()
  262. {
  263.     short    s;
  264.     
  265.     s = Spl(7);
  266.     G->flowOff &= ~0x40;
  267.     *SCCControl = 0x05;
  268.     *SCCControl = G->lastWR5 = G->lastWR5 | 0x80;
  269.     (void)Spl(s);
  270. }
  271.  
  272.  
  273. //
  274. //    Control 18 - Negate DTR.
  275. //
  276. NegateDTR()
  277. {
  278.     short    s;
  279.     
  280.     s = Spl(7);
  281.     G->flowOff |= 0x40;
  282.     *SCCControl = 0x05;
  283.     *SCCControl = G->lastWR5 = G->lastWR5 & ~0x80;
  284.     (void)Spl(s);
  285. }
  286.  
  287. //
  288. //    Control 19 - Set PE char.
  289. //
  290. SetPEChar()
  291. {
  292.     short    s;
  293.  
  294.     s = Spl(7);
  295.     G->peChar = (csStuff[1] >> 8) & 0xFF;
  296.     G->altChar = csStuff[1] & 0x7F;
  297.     (void)Spl(s);
  298. }
  299.  
  300. //
  301. //    Control 20 - Set PE & Alt chars.
  302. //
  303. SetPEAltChar()
  304. {
  305.     short    s;
  306.  
  307.     s = Spl(7);
  308.     G->peChar = (csStuff[1] >> 8) & 0xFF;
  309.     G->altChar = csStuff[1] & 0xFF;
  310.     (void)Spl(s);
  311. }
  312.  
  313. //
  314. //    Control 21 - Force XOff.
  315. //
  316. ForceXOff()
  317. {    
  318.     G->xOffFlag = 1;
  319. }
  320.  
  321. //
  322. //    Control 22 - Unconditionally clear XOff.
  323. //
  324. ClearXOff()
  325. {    
  326.     short    s;
  327.     
  328.     s = Spl(7);
  329.     G->xOffFlag = 0;
  330.     tbeint();
  331.     (void)Spl(s);
  332. }
  333.  
  334. //
  335. //    Control 23 - Send XOn if XOff last sent.  Clear FlowOff State.
  336. //
  337. SendXOnIff()
  338. {    
  339.     short    s;
  340.     
  341.     s = Spl(7);
  342.     if (G->flowOff & 0x80)
  343.     {
  344.         G->flowOff &= 0x7F;
  345.         if (!G->sendXOnff)
  346.         {
  347.             G->sendXOnff = G->xOnChar;
  348.             if (!G->moreTx)
  349.                 tbeint();
  350.         }
  351.         else
  352.             G->sendXOnff = 0;
  353.     }
  354.     (void)Spl(s);
  355. }
  356.  
  357. //
  358. //    Control 24 - Unconditionally Send XOn.  Clear FlowOff State.
  359. //
  360. SendXOn()
  361. {    
  362.     short    s;
  363.     
  364.     s = Spl(7);
  365.     G->flowOff &= 0x7F;
  366.     G->sendXOnff = G->xOnChar;
  367.     if (!G->moreTx)
  368.         tbeint();
  369.     (void)Spl(s);
  370. }
  371.  
  372. //
  373. //    Control 25 - Send XOff if XOn last sent.  Set FlowOff State.
  374. //
  375. SendXOffIff()
  376. {    
  377.     short    s;
  378.     
  379.     s = Spl(7);
  380.     if (!(G->flowOff & 0x80))
  381.     {
  382.         G->flowOff |= 0x8F;
  383.         if (!G->sendXOnff)
  384.         {
  385.             G->sendXOnff = G->xOffChar;
  386.             if (!G->moreTx)
  387.                 tbeint();
  388.         }
  389.         else
  390.             G->sendXOnff = 0;
  391.     }
  392.     (void)Spl(s);
  393. }
  394.  
  395. //
  396. //    Control 26 - Unconditionally Send XOff.  Set FlowOff State.
  397. //
  398. SendXOff()
  399. {    
  400.     short    s;
  401.     
  402.     s = Spl(7);
  403.     G->flowOff |= 0x80;
  404.     G->sendXOnff = G->xOffChar;
  405.     if (!G->moreTx)
  406.         tbeint();
  407.     (void)Spl(s);
  408. }
  409.  
  410. //
  411. //    Control 27 - Reset SCC.
  412. //
  413. ResetSCC()
  414. {
  415.     short    s;
  416.     
  417.     s = Spl(7);
  418.     *SCCControl = 0x09;
  419. #ifdef    PORTA
  420.         *SCCControl = 0x80;
  421. #endif
  422. #ifdef    PORTB
  423.         *SCCControl = 0x40;
  424. #endif
  425.     (void)Spl(s);
  426. }
  427.  
  428.  
  429. //
  430. //    InitSCC - Utility to reset and reinitialize SCC.
  431. //
  432.  
  433. InitSCC()
  434. {
  435.     short    s;
  436.     
  437.     s = Spl(7);
  438.     *SCCControl = 0x09;
  439. #ifdef    PORTA
  440.     *SCCControl = 0x80;
  441. #endif
  442. #ifdef    PORTB
  443.     *SCCControl = 0x40;
  444. #endif
  445.     *SCCControl = 0x04;
  446.     *SCCControl = G->lastWR4;
  447.     *SCCControl = 0x01;
  448.     *SCCControl = 0x04;
  449.     *SCCControl = 0x03;
  450.     *SCCControl = G->lastWR3 & ~1;
  451.     *SCCControl = 0x05;
  452.     *SCCControl = G->lastWR5 & ~0x8A;
  453.     *SCCControl = 0x09;
  454.     *SCCControl = 0x01;
  455.     *SCCControl = 0x0A;
  456.     *SCCControl = 0x00;
  457.     *SCCControl = 0x0B;
  458.     *SCCControl = 0x50;
  459.     *SCCControl = 0x0C;
  460.     *SCCControl = G->baudLow;
  461.     *SCCControl = 0x0D;
  462.     *SCCControl = G->baudHigh;
  463.     *SCCControl = 0x0E;
  464.     *SCCControl = 0x02;
  465.     *SCCControl = 0x03;
  466.     *SCCControl = G->lastWR3;
  467.     *SCCControl = 0x05;
  468.     *SCCControl = G->lastWR5;
  469.     *SCCControl = 0x0E;
  470.     *SCCControl = 0x03;
  471.     *SCCControl = 0x0F;
  472.     *SCCControl = 0xA0;
  473.     *SCCControl = 0x01;
  474.     *SCCControl = 0x17;
  475.     *SCCControl = 0x09;
  476.     *SCCControl = 0x09;
  477.     (void)Spl(s);
  478. }